<?php

class SurveysCompleted extends HTech_Db_Table
{
    protected $_name = 'view_surveys_completed';
    protected $_primary = array('SurveyId','QuestionId','ClientId','Date');  

    protected function _setGridMetadata()
    {
        switch($this->_name) {
            case 'surveys_completed':
                // this case handles the export action
                $this->_gridMetadata['title'] = $this->translate("Surveys completed");
                $this->_gridMetadata['headers'] = array
                    (
                        array('SurveyId',       40, $this->translate("Survey"),   'Number'),
                        array('QuestionId',     70, $this->translate("Question"), 'Number'),
                        array('AnswerId',      270, $this->translate("Answer"),   'Number'),
                        array('ClientId',      270, $this->translate("Client"),   'Number'),
                        array('OfficerUserId', 270, $this->translate("Officer"),  'Number'),
                        array('Date',          270, $this->translate("Date"),     'DateTime', 'Date'),
                        array('Cycle',         470, $this->translate("Cycle"),    'Number')
                    );
                break;
            default:
                $this->_gridMetadata['title'] = $this->translate("Surveys completed");
                $this->_gridMetadata['onClick'] = 'survey.question.view';
                $this->_gridMetadata['onClickParams'] = array('SurveyId','QuestionId');
                $this->_gridMetadata['params'] = array('ClientId','SurveyId','QuestionId');
                $this->_gridMetadata['headers'] = array
                    (
                        array('ProductLine',    150, $this->translate("Product line"), 'String'),
                        array('Program',        200, $this->translate("Program"),      'String'),
                        array('Survey',         200, $this->translate("Survey"),       'String'),
                        array('Question',       300, $this->translate("Question"),     'String'),
                        array('Answer',         300, $this->translate("Answer"),       'String'),
                        array('Score',           80, $this->translate("Score"),        'String'),
                        array('ClientFullName', 150, $this->translate("Client"),       'String'),
                        array('Date',            80, $this->translate("Date"),         'DateTime', 'Date'),
                        array('Cycle',           80, $this->translate("Cycle"),        'Number'),
                        array('SurveyId',         1, $this->translate("Survey"),       'Number'),
                        array('QuestionId',       1, $this->translate("Question"),     'Number'),
                        array('AnswerId',         1, $this->translate("Answer"),       'Number'),
                        array('ClientId',         1, $this->translate("Client"),       'Number'),
                        array('OfficerUserId',    1, $this->translate("Officer"),      'Number')
                    );
        }
        return $this;
    }
    
    static function hitsByCycle($question,$survey=null,$client=null)
    {
        $surveysCompleted = new SurveysCompleted();
        $query = $surveysCompleted->select()
                     ->from($surveysCompleted, array('AnswerId', 'Cycle', 'Hits' => 'count(*)'))
                     ->where('SurveyId = ?', $survey);
        if($survey) $query->where('QuestionId = ?', $question);
        if($client) $query->where('ClientId = ?', $client);
        $query->group(array('AnswerId','Cycle'));
        return $surveysCompleted->fetchAll($query);
    }
 
}